package cn.itcast.service.impl;

import cn.itcast.dao.CountyDao;
import cn.itcast.domain.County;
import cn.itcast.service.CountyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class CountyServiceImpl implements CountyService{
    @Override
    public List<County> findByName(String name) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        CountyDao countyDao = sqlSession.getMapper(CountyDao.class);
        List<County> list = countyDao.findByName("%"+name+"%");
        return list;
    }

    @Override
    public PageInfo<County> findAll(Integer pageNum,Integer pageSize) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        CountyDao countyDao = sqlSession.getMapper(CountyDao.class);
        PageHelper.startPage(pageNum,pageSize);
        List<County> list = countyDao.findAll();
        PageInfo<County> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
